enabledebug = false;
var MyDebug = {
	info: function(method, message){
		if(enabledebug)alert(method + ': ' + message);
	}
};
var LGridInsertRowCleaner = {
	
	selectorTemplate: [
       new Template('input[name="#{name}"]'),
       new Template('textarea[name="#{name}"]'),
       new Template('select[name="#{name}"]')
   ],	

	getSelectors: function(name){
		MyDebug.info('LGridInsertRowCleaner-getSelectors', name);
		var opt = {name: name};
		MyDebug.info('getSelectors', opt.name);
	
		var result = [];
		LGridInsertRowCleaner.selectorTemplate.each(function(item, index){
			result.push(item.evaluate(opt));
		});
		MyDebug.info('LGridInsertRowCleaner-getSelectors', result);
		return result;
	},
	clearFields:function(sourceItemsId, trId){
		var trSource = $(trId);
	//	MyDebug.info('clearFields' + sourceEl);
		MyDebug.info('LGridInsertRowCleaner-clearFields', sourceItemsId);
		sourceItemsId.each(
			function(item, index){
				MyDebug.info('LGridInsertRowCleaner-clearFields', item);
				if(item){
					var sources = trSource.getElementsBySelector(
							LGridInsertRowCleaner.getSelectors(item));
					LGridInsertRowCleaner.clearField(sources);
				}
			});
	},

	clearField:function(sourceElements){
		MyDebug.info('LGridInsertRowCleaner-clearField', sourceElements);
		if(sourceElements && sourceElements[0] && sourceElements[0].name){
//			MyDebug.info('[name="' + source.name + '"]');
//			MyDebug.info(this.destForm.getElementsBySelector('[name="' + source.name + '"]'));
			MyDebug.info('LGridInsertRowCleaner-clearField', sourceElements[0].name);
			var tagName = sourceElements[0].tagName.toLowerCase();
			
			if(tagName == 'input'){
//				MyDebug.info(source.type)
				var type = sourceElements[0].type;
				if(type == "text"){
					sourceElements[0].value = '';
				}else if(type == "checkbox"){
					//
				}else if(type == "radio"){
					//
				}
			}else if(tagName == 'textarea'){
				//
			}else if(tagName == 'select' || tagName == 'selectmultiple'){
				sourceElements[0].options[0].selected = true;
			}else{
				MyDebug.info('LGridInsertRowCleaner-clearField', tagName);
			}
		}
	}		
};
var LGridInsertRow = Class.create();
LGridInsertRow.prototype = {
	sourceItemsId: [],
	
	destForm: '',
	
	selectorTemplate: [
	                   new Template('input[name="#{name}"]'),
	                   new Template('textarea[name="#{name}"]'),
	                   new Template('select[name="#{name}"]')
					],
	                   
	initialize: function(sourceItemsId, destId){
		MyDebug.info('initialize', sourceItemsId);
		MyDebug.info('initialize', destId);
		this.sourceItemsId = sourceItemsId;
//		MyDebug.info('initialize', $('insertRow_td').getElementsBySelector('form')[0]);
		MyDebug.info('initialize', $(destId));
//		this.destForm = $('insertRow_td').getElementsBySelector('form')[0];
		
		this.destForm = $(destId);
	},
	
	copyFields:function(sourceEl){
		var trSource = this.getParentTr(sourceEl);
//		MyDebug.info('copyFields' + sourceEl);
		MyDebug.info('copyFields', this.sourceItemsId);
		this.sourceItemsId.each(
				function(item, index){
					MyDebug.info('copyFields', item);
					if(item){
						this.copyField(
								trSource.getElementsBySelector(
										this.getSelectors(item)), item);
					}
				},
				this);
	},
	
	submitForm:function(){
//		var cb = this.destForm.getElementsBySelector('[type="submit"]');
//		MyDebug.info('submitForm', cb);
//		Simulator.simulateClick(cb[0]);
		//destForm.
	},
	
	getParentTr:function(source){
		var result = null;
		source.ancestors().each(function(item, index){
			if(item.tagName.toLowerCase() == 'tr'){
				result = item;
//				throw $break;
			}
		});
		return result;
	},
	
	copyField:function(sourceElements, name){
		MyDebug.info('copyField', sourceElements);
		if(sourceElements && sourceElements[0] && sourceElements[0].name){
//			MyDebug.info('[name="' + source.name + '"]');
//			MyDebug.info(this.destForm.getElementsBySelector('[name="' + source.name + '"]'));
			MyDebug.info('copyField', sourceElements[0].name);
			var selectors = this.getSelectors(name);
			MyDebug.info('copyField', selectors);
			var destElements = this.destForm.getElementsBySelector(selectors);
			var tagName = sourceElements[0].tagName.toLowerCase();
			
			if(tagName == 'input'){
//				MyDebug.info(source.type)
				var type = sourceElements[0].type;
				if(type == "text"){
					this.copyText(sourceElements, destElements);
				}else if(type == "checkbox"){
					this.copyCheck(sourceElements, destElements);
				}else if(type == "radio"){
					this.copyRadio(sourceElements, destElements);
				}
			}else if(tagName == 'textarea'){
				this.copyTextarea(sourceElements, destElements);
			}else if(tagName == 'select' || tagName == 'selectmultiple'){
				this.copySelect(sourceElements, destElements);
			}else{
				MyDebug.info('copyField', tagName);
			}
		}
	},
	
	getSelectors: function(name){
		MyDebug.info('getSelectors', name);
		var opt = {name: name};
		MyDebug.info('getSelectors', opt.name);

		var result = [];
		this.selectorTemplate.each(function(item, index){
			result.push(item.evaluate(opt));
		});
		MyDebug.info('getSelectors', result);
		return result;
	},
	
	copyText: function(source, dests){
		MyDebug.info('copyText', dests[0].value);
		MyDebug.info('copyText', source[0].value);
		dests[0].value = source[0].value;
		source[0].value = '';
	},
	
	copyCheck: function(source, dests){
		MyDebug.info('copyCheck', source[0].form);
		
	},
	
	copyRadio: function(source, dests){
		MyDebug.info('copyCheck', source[0].form);
	},
	
	copyTextarea: function(source, dests){
		dests[0].value = source[0].value;
	},
	
	copySelect: function(source, dests){
		var selectedIndexes = [];
		MyDebug.info('copySelect', source);
		MyDebug.info('copySelect', dests);
		for(var i=0; i < source[0].options.length; i++){
			if(source[0].options[i].selected)selectedIndexes.push(i);
		}
		source[0].options[0].selected = true;
		selectedIndexes.each(function(item, index){
			dests[0].options[item].selected = true;
		});
		
	}
};